package com.papercutting.platform.controller;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.entity.UserLike;
import com.papercutting.platform.service.UserLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户点赞控制器
 */
@RestController
@RequestMapping("/api/like")
@RequiredArgsConstructor
public class UserLikeController {
    
    private final UserLikeService userLikeService;
    
    /**
     * 获取用户点赞列表
     */
    @GetMapping("/list")
    public Result<List<UserLike>> getUserLikes(@RequestHeader("Authorization") String token) {
        // 这里应该从token中解析用户ID，暂时用固定值
        Long userId = 1L;
        
        List<UserLike> likes = userLikeService.findByUserId(userId);
        return Result.success(likes);
    }
    
    /**
     * 添加或取消点赞
     */
    @PostMapping("/toggle")
    public Result<Map<String, Object>> toggleLike(
            @RequestHeader("Authorization") String token,
            @RequestParam String targetType,
            @RequestParam Long targetId) {
        
        // 这里应该从token中解析用户ID，暂时用固定值
        Long userId = 1L;
        
        boolean isLiked = userLikeService.toggleLike(userId, targetType, targetId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("liked", isLiked);
        result.put("message", isLiked ? "点赞成功" : "取消点赞成功");
        
        return Result.success(result);
    }
    
    /**
     * 检查是否已点赞
     */
    @GetMapping("/check")
    public Result<Map<String, Object>> checkLike(
            @RequestHeader("Authorization") String token,
            @RequestParam String targetType,
            @RequestParam Long targetId) {
        
        // 这里应该从token中解析用户ID，暂时用固定值
        Long userId = 1L;
        
        UserLike like = userLikeService.findByUserIdAndTargetTypeAndTargetId(userId, targetType, targetId);
        boolean isLiked = like != null;
        
        Map<String, Object> result = new HashMap<>();
        result.put("liked", isLiked);
        
        return Result.success(result);
    }
}